//import getAppsubject from './getData'
/**
 * 存储localStorage
 */


export const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
export const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
    if (!name) return;
    return window.localStorage.removeItem(name);
}
/**
 * 存储cookie
 */
export const setCookie = (name, value, isSetTime) =>  {
	var host = location.hostname;
    if (isSetTime === undefined || isSetTime === true) {
        var day = 30;//默认cookie保存30天
        if(name=='GUID'){
            day = 1;
        }
        var date = new Date();
        date.setTime(date.getTime() + Number(day) * 24 * 60 * 60 * 1000);
        if(host === 'wide.ksbao.com') {
            document.cookie = name + "=" + escape(value) + ";domain=ksbao.com; path=/;expires = " + date.toGMTString();
        }else{
            document.cookie = name + "=" + escape(value) + "; path=/;expires = " + date.toGMTString();
        }
    }
    else {
        if(host === 'wide.ksbao.com') {
            document.cookie = name + "=" + escape(value) + ";domain=ksbao.com; path=/";
        }else{
            document.cookie = name + "=" + escape(value) + "; path=/";
        }
    }
}
/**
 * 获取cookie
 */
export const getCookie = name =>  {
	if (!name) return;
    var value = null;
    var arr;
    var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    arr = document.cookie.match(reg);
    if (arr) {
        value = unescape(arr[2]);
    }
    return (value) ? value : null;
}

/**
 * 删除cookie
 */
export const removeCookie = name =>  {
	var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=;path=/;domain=ksbao.com;expires = " + date.toGMTString();
    document.cookie = name + "=;path=/;expires = " + date.toGMTString();
}

/**
 * 删除所有cookie
 */
export const cleanCookie = () =>  {
	var strCookie = document.cookie;
    var arrCookie = strCookie.split('; '); // 将多cookie切割为多个名/值对
    for (var i = 0; i < arrCookie.length; i++) { // 遍历cookie数组，处理每个cookie对
        var arr = arrCookie[i].split("=");
        if (arr.length > 0) {
            this.removeCookie(arr[0]);
        }
    }
}
/**
 * 存储appName, appEName, appID ,appvn ,appvname
 * 创建信息：赵梦琦（2018-12-26）
 */
export const setSelectedApp = (appName, appEName, appID ,appvn ,appvname) =>{
	let newSelected = {
        AppName: appName,
        AppEName: appEName,
        AppID: appID,
        AppVn:appvn,
        AppVname:appvname
    }
	let json = JSON.stringify(newSelected);
    setCookie('SELECTEDAPP', json);
}
/**
 * 获取appName
 * 创建信息：赵梦琦（2018-12-26）
 */
export const getAppName = () =>{
	let selectedApp = getCookie('SELECTEDAPP');
    if (!selectedApp) {
        return null
    }
    let appArr = JSON.parse(selectedApp);
    return appArr.AppName;
}
/**
 * 获取 appEName
 * 创建信息：赵梦琦（2018-12-26）
 */
export const getAppEName = () =>{
	let selectedApp = getCookie('SELECTEDAPP');
    if (!selectedApp) {
        return null
    }
    let appArr = JSON.parse(selectedApp);
    return appArr.AppEName;
}
/**
 * 获取appID 
 * 创建信息：赵梦琦（2018-12-26）
 */
export const getAppID = () =>{
	let selectedApp = getCookie('SELECTEDAPP');
    if (!selectedApp) {
        return null
    }
    let appArr = JSON.parse(selectedApp);
    return appArr.AppID;
}
/**
 * 获取 appvn 
 * 创建信息：赵梦琦（2018-12-26）
 */
export const getAppVN = () =>{
	let selectedApp = getCookie('SELECTEDAPP');
    if (!selectedApp) {
        return null
    }
    let appArr = JSON.parse(selectedApp);
    return appArr.AppVn;
}
/**
 * 获取appvname
 * 创建信息：赵梦琦（2018-12-26）
 */
export const getAppAppVName = () =>{
	let selectedApp = getCookie('SELECTEDAPP');
    if (!selectedApp) {
        return null
    }
    let appArr = JSON.parse(selectedApp);
    return appArr.AppVname;
}

/**
 * 检测是否为手机号
 */
export const checkCellPhone = phone => {
    if (phone.length !== 0) {
        let myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        return myreg.test(phone);
    }
}

/**
 *  获取浏览器类型和版本号
 *  返回值：string 浏览器名称和版本（空格分隔开）
 *  创建信息：赵梦琦(2018-12-7)
 */
export const getBrowserVersion = () =>{
	var browser = {};
    var userAgent = navigator.userAgent.toLowerCase();
    var verString;
    (verString = userAgent.match(/msie ([\d.]+)/))
        ? browser.ie = verString[1]
        : (verString = userAgent.match(/firefox\/([\d.]+)/))
        ? browser.firefox = verString[1]
        : (verString = userAgent.match(/chrome\/([\d.]+)/))
        ? browser.chrome = verString[1]
        : (verString = userAgent.match(/opera.([\d.]+)/))
        ? browser.opera = verString[1]
        : (verString = userAgent
        .match(/version\/([\d.]+).*safari/))
        ? browser.safari = verString[1]
        : 0;
    var version = '';
    if (browser.ie) {
        version = 'msie ' + browser.ie;
    } else if (browser.firefox) {
        version = 'firefox ' + browser.firefox;
    } else if (browser.chrome) {
        version = 'chrome ' + browser.chrome;
    } else if (browser.opera) {
        version = 'opera ' + browser.opera;
    } else if (browser.safari) {
        version = 'safari ' + browser.safari;
    } else {
        version = '未知的浏览器类型';
    }
    return version;
}
/**
 * 判断设备类型
 */
export const clientType = () => {
    var browser = {
        versions: function() {
            var u = navigator.userAgent;
            return {
                capsule: u.indexOf('yingteng') > -1,
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
            };
        }()
    };
    if (browser.versions.capsule) {
        if (browser.versions.iPhone || browser.versions.iPad) {
            return 'web_iPhone';
        }
        if (browser.versions.ios || browser.versions.android || browser.versions.webApp) {
            return 'web_android';
        }
    } else {
        if (getStore('iosCookie') && getStore('iosCookie') === 'iosKe') {
            if (browser.versions.iPhone || browser.versions.iPad) {
                return 'web_iPhone';
            }
        } else {
            if (browser.versions.iPhone || browser.versions.iPad) {
                return 'hej_iPhone';
            }
            if (browser.versions.ios || browser.versions.android || browser.versions.webApp) {
                return 'hej_android';
            }
        }
    }
    return 'web_pc';
}

/**
 *  对象中的属性值比较大小
 *  输入参数：对象中的属性名字
 *  返回值：排好序的对象属性值 从小到大
 *  创建信息：赵梦琦(2018-12-11)
 */
export const compareObj = (propertyName) =>{
    console.log(propertyName)
	return function(object1, object2) {
      	var value1 = object1[propertyName];
      	var value2 = object2[propertyName];
      	if (value2 < value1) {
      		return 1;
      	} else if (value2 > value1) {
        	return -1;
      	} else {
        	return 0;
      	}
    }
}
/**
 *  格式化时间
 *  输入参数：datetime(时间) format 格式化字符串 (yyyy/MM/dd hh:mm:ss)
 *  返回值：string 格式化后的字符串 
 *  创建信息：赵梦琦(2018-12-11)
 */
export const datetimeFormat = (datetime, format) => {
    var timeSplitT='';
    var timeSplitDate='';
    var timeSplitTime='';

    if(datetime){
        if(typeof (datetime)==='string'){
			datetime=datetime.replace('T', ' ');
			datetime=datetime.replace(/-/g, '/');
			datetime=datetime.replace('.000Z', '');
            datetime=new Date(datetime);
			var time=Date.parse(datetime);
			time=time+8*3600*1000;
			datetime=new Date(time);
        }
    }else{
        return '';
    }
    var date = {
        "M+": datetime.getMonth()+1, //月份
        "d+": datetime.getDate(), //日
        "h+": datetime.getHours(), //小时
        "m+": datetime.getMinutes(), //分
        "s+": datetime.getSeconds(), //秒
        "q+": Math.floor((datetime.getMonth() + 3) / 3), //季度
        "S": datetime.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (date[k]) : (('00' + date[k]).substr(('' + date[k]).length)));
        }
    }
    return format;
}

/**
 *  格式化时间
 *  输入参数：datetime(时间) format
 *  返回值：string 格式化后的字符串
 *  创建信息：赵梦琦(2018-12-11)
 */
export const compareTime = (datetime, datetime2) => {
    let a = new Date(datetime);
    let b = new Date();
    if(datetime2){
        b= new Date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HNKX_YTMJ','ZC_ZXYJHWK_YTMJ','ZC_ZXYJHGSK_YTMJ','ZC_KQZGJS_YTMJ','ZC_FSYXZGJS_YTMJ','ZC_CSBYXJS_YTMJ','ZC_XLZLX_YTMJ','ZC_LCYXJYJS_YTMJ','ZC_BLXJS_YTMJ','ZC_KFYXZLJS_YTMJ','ZC_LHJYJS_YTMJ','ZC_WSWJYJS_YTMJ','ZC_HYXJS','ZC_XDJS','ZC_XDXJS_YTMJ','ZC_ZLFSZLJS','ZC_BAXSJS','ZC_SXJS_YTMJ','ZY_SJDSWJS','ZC_YRX_YTMJ','ZC_ZGHS_YTMJ','ZC_ZGHSNKHL_YTMJ','ZC_ZGHSWKHL_YTMJ','ZC_ZGHSEKHL_YTMJ','ZC_ZGHSSQHL_YTMJ','ZC_ZGHSFKHL_YTMJ','ZC_ZYHLX','ZC_YS_YTMJ','ZC_ZGZYS_YTMJ']
    // 初级科目
    let primarykemu=['ZC_HLXHS_YTMJ','ZC_ZYHLXHS','ZC_KQYXFSS_YTMJ','ZY_KQYXJSS_YTMJ','ZC_FSYXJSS_YTMJ','ZC_FSYXJS_YTMJ','ZC_JYJSS_YTMJ','ZC_JYCJ_YTMJ','ZY_BLXJSS','ZY_KFYXZLJSS_YTMJ','ZY_KFYXJSS_YTMJ','ZY_YYSI_YTMJ','ZY_YYS_YTMJ','ZC_LHJYJSI_YTMJ','ZC_LHJYJSS_YTMJ','ZY_WSWJYJS_YTMJ','ZY_WSWJYJSS_YTMJ','ZY_BLXJS_YTMJ','ZC_XLZLS_YTMJ','ZC_BAXXJSS','ZC_BAXXJS','ZY_SXJSS_YTMJ','ZY_NDTJSS','ZC_YJSHI_YTMJ','ZC_YJS_YTMJ','ZC_ZYAOSHI_YTMJ','ZC_ZYS_YTMJ'];
    if(appName && (appName.indexOf('正高')>-1 || appName.indexOf('副高')>-1)){
        return 'gaoji';
    }else if(intermediatekemu.indexOf(appEName) >-1){
        return 'zhongji';
    }else if(primarykemu.indexOf(appEName) >-1){
        return 'chuji'
    }else{
        return 'other'
    }
}
/**
 *  存储 LocalStorage
 *  输入参数：name参数名 value参数值
 *  创建信息：赵梦琦(2018-12-15)
 */
export const setLocalStorageValue = (name, value) => {
    localStorage.setItem(name, JSON.stringify(value));
}
/**
 *  获取 LocalStorage
 *  输入参数：name参数名
 *  创建信息：赵梦琦(2018-12-15)
 */
export const getLocalStorageValue =  (name) => {
    return JSON.parse(localStorage.getItem(name));
}
/**
 *  移除 LocalStorage
 *  输入参数：name参数名
 *  创建信息：赵梦琦(2018-12-15)
 */
export const removeLocalStorageValue =  (name) =>{
    return JSON.parse(localStorage.getItem(name));
}


